package com.kly.chatter.api.application.service.impl;

import cn.hutool.core.date.DateUtil;
import com.fqgj.common.api.Page;
import com.kly.chatter.api.application.dto.ChatterRewardReq;
import com.kly.chatter.api.application.dto.ChatterRewardResp;
import com.kly.chatter.api.application.dto.RewardInfoDto;
import com.kly.chatter.api.application.service.ChatterRewardService;
import com.kly.chatter.db.dao.ChatterRewardInfoDao;
import com.kly.chatter.db.entity.ChatterRewardInfoEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ChatterRewardServiceImpl implements ChatterRewardService {


    @Resource
    ChatterRewardInfoDao chatterRewardInfoDao;

    @Override
    public ChatterRewardResp listChatterReward(ChatterRewardReq req, Page page) {
        final Integer chatterRewardInfoCnt = chatterRewardInfoDao.getChatterRewardInfoCnt(req.getChatterAccount(), req.getChatterNickName(), DateUtil.parseDateTime(req.getStartDate()), DateUtil.parseDateTime(req.getEndDate()), req.getChatterCodes());
        if (chatterRewardInfoCnt == 0) {
            return ChatterRewardResp.builder().totalNum(0).build();
        }

        final List<ChatterRewardInfoEntity> chatterRewardInfoEntities = chatterRewardInfoDao.listChatterRewardInfo(req.getChatterAccount(), req.getChatterNickName(), DateUtil.parseDateTime(req.getStartDate()), DateUtil.parseDateTime(req.getEndDate()), page, req.getChatterCodes());
        final List<RewardInfoDto> rewardInfoDtos = chatterRewardInfoEntities.stream().map(o -> {
            RewardInfoDto dto = RewardInfoDto.builder().build();
            BeanUtils.copyProperties(o, dto);
            dto.setPppvAmountTotal(o.getPpRewardAmount().add(o.getPvRewardAmount()));
            return dto;
        }).collect(Collectors.toList());

        return ChatterRewardResp.builder().totalNum(chatterRewardInfoCnt).rewardInfoDtoList(rewardInfoDtos).build();
    }

}
